package net.xdclass.controller;


import com.baomidou.mybatisplus.extension.api.R;
import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.LinkGroupService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2022-05-09
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /*
    创建短链分组
     */
    @RequestMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest request) {
        int rows = linkGroupService.add(request);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    /*
    根据短链分组Id删除
     */
    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id") Long groupId) {
        int row = linkGroupService.del(groupId);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /*
    根据短链分组Id查看详情
     */
    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId) {
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }
    /*
    根据短链分组Id查看详情
     */
    @GetMapping("list")
    public JsonData findUserAllLinkGroup() {
        List<LinkGroupVO> list = linkGroupService.listAllGroup();
        return JsonData.buildSuccess(list);
    }
    /*
    根据短链分组Id修改
     */
    @PostMapping("update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request) {

        int rows =linkGroupService.updateById(request);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }
}

